<template>
  <!-- 修改盘点单对话框 -->
  <el-dialog v-model="visible" title="修改盘点单单" width="400px" @close="close" destroy-on-close>
    <el-form ref="checkUpdateForm" :model="checkUpdate" :rules="rules" label-position="top" label-width="120px">
      <el-form-item label="盘点单号：" prop="buyId">
        <el-input v-model="checkUpdate.buyId" disabled />
      </el-form-item>
      <el-form-item label="仓库：" prop="storeName">
        <el-input v-model="checkUpdate.storeName" disabled />
      </el-form-item>
      <el-form-item label="商品名称：" prop="productName">
        <el-input v-model="checkUpdate.productName" disabled />
      </el-form-item>
      <el-form-item label="实际盘点数量：" prop="buyNum">
        <el-input v-model="checkUpdate.checkNum" controls-position="right" />
      </el-form-item>
      <el-form-item label="是否损坏及详细情况：" prop="checkBreakdown">
        <el-input v-model="checkUpdate.checkBreakdown" controls-position="right" />
      </el-form-item>
      <el-form-item label="盘点人：" prop="checkUser">
        <el-input v-model="checkUpdate.checkUser" controls-position="right" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="updateCheck">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { put, tip } from "@/common"
import { useRouter } from 'vue-router';

// 在你的`setup`函数中
const router = useRouter();

// 该页面的可见性
const visible = ref(false);

// 添加盘点单对象
const checkUpdate = reactive({
  checkId: '',
  storeName: '',
  productName: '',
  checkNum: '',
  checkBreakdown: '',
  checkUser: ''
});

// 表单验证规则
const rules = reactive({
  checkNum: [
    { required: true, message: '请输入盘点数量', trigger: 'blur' }
  ],
  checkUser: [
    { required: true, message: '请输入采购人', trigger: 'blur' }
  ],
  checkBreakdown: [
    { required: true, message: '请详细描述损坏情况', trigger: 'blur' }
  ],
})

// 关闭
const close = () => {
  visible.value = false;
}

// 该对话框打开，进行数据初始化
const open = (check) => {
  for(let prop in check){
    checkUpdate[prop] = check[prop];
  }
  visible.value = true;
};

const checkUpdateForm = ref();
// 定义
const emit = defineEmits(["ok"]);
// 添加盘点单提交
const updateCheck = () => {
  checkUpdateForm.value.validate((valid) => {
    if(valid){
      put('/check/check-update', checkUpdate).then(result => {
        emit('ok');
        tip.success(result.message);

        visible.value = false; // 关闭对话框
        // 跳转到分页查询页面
        router.push({ name: '/inventory/checklist' });
      });
    }
  });
}


defineExpose({ open });
</script>

<style>


</style>